﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

//http://www.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx

namespace Medianamik.UI.Web.Services
{
    public delegate void UseServiceDelegate<T>(T proxy);
    public static class Service<T>
    {
        public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>("*");
        public static void Use(UseServiceDelegate<T> codeBlock)
        {
            var proxy = (IClientChannel)_channelFactory.CreateChannel();
            var success = false;
            
            try
            {
                codeBlock((T)proxy); proxy.Close(); success = true;
            }
            finally { if (!success) { proxy.Abort(); } }
        }
    }
}
